import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
//import java.util.Scanner;

public class lineRecognition {

    public ArrayList<Integer> outlineLines(String filename){
       
        File inputFile = new File(filename);

        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(inputFile);
        } catch (IOException ex) {
            System.out.println(ex);
        }

        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();
        
        System.out.println("w: " + w + " h: " + h);

        // create array to store pixels
        int[] pixels = new int[w*h];

        bufferedImage.getRGB(0, 0, w, h, pixels, 0, w); //Get all pixels

        ArrayList<Integer> changes = new ArrayList<Integer>();
        
        int alpha = 800000;

        // we don't need to loop to the last row because pixels[i+w] will check it
        for (int i = 0; i < pixels.length-w-1; i++) {

            if(i%w != (w-1)){

                if(Math.abs(Math.abs(pixels[i]) - Math.abs(pixels[i+1])) > alpha){

                    changes.add(i);
                }

                if(Math.abs(Math.abs(pixels[i]) - Math.abs(pixels[i+w])) > alpha){

                    changes.add(i);
                }

            }
            
        } // end pixel for-loop

        int red = 0xFFFF0000;

        // make pixel color changes
        for(int i=0; i < changes.size(); i++){
        
            bufferedImage.setRGB(changes.get(i)%w, changes.get(i)/w, red);
        }

        // create a new file and write out the new image
        int dot = filename.indexOf(".");

        String name = filename.substring(0, dot);
        String ext = filename.substring(dot+1, filename.length());
        
        File output = new File("traced"+name+""+alpha+"."+ext);
        
        try{
            ImageIO.write(bufferedImage, ext, output);
        } catch (Exception e){}

        return changes;
    } // end outlineLines

    public void justTheOutLine(String filename, ArrayList<Integer> p){

        File inputFile = new File(filename);

        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(inputFile);
        } catch (IOException ex) {
            System.out.println(ex);
        }

        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();

        System.out.println("w: " + w + " h: " + h);

        // create array to store pixels
        int[] pixels = new int[w*h];

        bufferedImage.getRGB(0, 0, w, h, pixels, 0, w); //Get all pixels

        int alpha = 800000;
        int red = 0xFFFF0000;
        int black = 0x00000000;

        // black out the image
        for(int i=0; i < pixels.length; i++){

            bufferedImage.setRGB(i % w, i / w, black);
        }

        // put the red line markings back
        for (int i = 0; i < p.size(); i++) {

            bufferedImage.setRGB(p.get(i) % w, p.get(i) / w, red);
        }

        // create a new file and write out the new image
        int dot = filename.indexOf(".");

        String name = filename.substring(0, dot);
        String ext = filename.substring(dot+1, filename.length());

        File output = new File("LINE"+name+""+alpha+"."+ext);

        try{
            ImageIO.write(bufferedImage, ext, output);
        } catch (Exception e){}

    }

} // end class